﻿using Endos.Wodo.Domain;
using System;
using System.Linq;

namespace Endos.Wodo.Persistence
{
    internal class Query<T> : IQuery<T>
        where T : Entity
    {
        private readonly IQueryable<T> _queryable;
        private readonly QueryableFetch<T> _queryableFetch;

        public Query(IQueryable<T> queryable, Action<IFetch<T>> action = null)
        {
            _queryable = queryable;
            _queryableFetch = new QueryableFetch<T>();
            if (action != null)
            {
                action(_queryableFetch);
            }
        }

        public IFetch<T> Fetch
        {
            get { return _queryableFetch; }
        }

        public IQueryable<T> Queryable
        {
            get
            {
                return _queryableFetch.Configure(_queryable);
            }
        }
    }
}
